﻿package com.codingbros.zigzag
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
		
	import flash.events.Event;
		
	import com.codingbros.zigzag.lines.ILine;
	import com.codingbros.zigzag.lines.Line;
	import com.codingbros.zigzag.lines.MovingLine;

	public class Zigzag extends Sprite 
	{
		
		private var myZigZagLines:Array;
		private const ZIGZIGNUM:int = 100;
		
		private var updateNum:int = 0;
		private const MAXUPDATES:int = 650;


		public function Zigzag() {
			trace("test01");
			
			if(stage) {
				onInit();
			} else {
				this.addEventListener(Event.ADDED_TO_STAGE, onInit);
			}
		}
		
		private function onInit(e:Event = null):void 
		{
			trace("running");
			
			myZigZagLines = new Array(ZIGZIGNUM);
			
			for (var i:int = 0; i < myZigZagLines.length; i ++) 
			{
				//var tempZigZagLine:ILine = new Line(stage.stageWidth);
				var tempZigZagLine:ILine = new MovingLine(stage.stageWidth, stage.stageHeight);
				
				this.addChild(tempZigZagLine as DisplayObject);
				
				myZigZagLines[i] = tempZigZagLine;
			}			
			this.addEventListener(Event.ENTER_FRAME, updateGraphics);			
		}
		
		private function updateGraphics(e:Event):void 
		{			
			for (var i:int = 0; i < myZigZagLines.length; i ++) 
			{
				myZigZagLines[i].update();
				myZigZagLines[i].draw();
			}
			
			updateNum++;
			
			if (updateNum >= MAXUPDATES) {
				this.removeEventListener(Event.ENTER_FRAME, updateGraphics);
			}
		}
	}
}


//
//
//
//
//
//
//
//
//
//
//
//